package hex;

import java.util.ArrayList;
import java.util.ListIterator;

public class CodeCache {
	// private HashMap<Integer, Instruction> codecache = new HashMap<Integer,
	// Instruction>();
	private static CodeCache instance = new CodeCache();
	ArrayList<Instruction> codecache = new ArrayList<Instruction>();
	private int current_ip = 0;
	private int orignal_ip_base = 0;

	public static CodeCache getInstance() {
		return instance;
	}

	public void add(Instruction p) {
		p.original_ip = orignal_ip_base + Input.getInstance().getBeforeIndex();
		p.ip = current_ip;
		codecache.add(p);
		current_ip += p.GetLength();
		if (p.GetLength() > 0) {
			Input.getInstance().SetIndex(
					Input.getInstance().getBeforeIndex() + p.GetLength());
			System.out.println("before " + Input.getInstance().getBeforeIndex()
					+ " index:" + Input.getInstance().getIndex() + " p_length:"
					+ p.GetLength());
			Input.getInstance().resetBeforeIndex();
		}

	}

	public void visit() {
		for (ListIterator<Instruction> p = codecache.listIterator(); p
				.hasNext();) {
			System.out.println(p.next().toString());
		}
	}
}
